package com.mengshuai.mysql.controller.mongo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookRepository bookRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    public List<Book> findByAuthor1(String author) {
        Query query = new Query(Criteria.where("author").is(author));
        return mongoTemplate.find(query, Book.class);
    }

    public List<Book> findByAuthor(String author) {
        return bookRepository.findByAuthor(author);
    }

    public void insertOne(Book book){
        bookRepository.insert(book);
    }

    public void deleteOne(String id){
        bookRepository.deleteById(id);
    }

    public void updateOne(Book book){
        bookRepository.save(book);
    }
}
